<template>
 <div>
   	<!-- 面包屑导航区 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>系统管理</el-breadcrumb-item>
			<el-breadcrumb-item>角色管理</el-breadcrumb-item>
		</el-breadcrumb>
    <el-card>
			<!-- 搜索添加区 -->
			<el-row :gutter="20">
				<el-form :inline="true" :model="queryInfo" class="demo-form-inline">
					<el-form-item label="角色名称">
						<el-input v-model="queryInfo.Name" placeholder=""></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit">查询</el-button>
					</el-form-item>
					<el-button type="primary" @click="addDialogVisible=true">添加</el-button>
				</el-form>
				
			</el-row>
			<!--用户列表区  -->
			<el-table :data="roleslist" border stripe>
				<el-table-column label="编号" prop="Id"></el-table-column>
				<el-table-column label="角色名称" prop="Name"></el-table-column>
				<el-table-column label="操作" >
					<template v-slot="scope">
						<!-- 分配角色按钮 -->
						<el-tooltip effect="dark" content="分配模块" placement="top" :enterable="false">
							<el-button type="warning" icon="el-icon-setting" size="mini" @click="setRole(scope.row.Id)"></el-button>
						</el-tooltip>
						<!-- 修改按钮 -->
						<el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.Id)"></el-button>
						<!-- 删除按钮 -->
						<el-button type="danger" icon="el-icon-delete" size="mini" @click="removeRoleById(scope.row.Id)"></el-button>
						<!-- <el-tooltip effect="dark" content="重置密码" placement="top" :enterable="false">
								<el-button type="info" icon="el-icon-refresh" size="mini" @click="refreshById(scope.row.Id)"></el-button>
						</el-tooltip> -->
					
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
			 :page-sizes="[5, 10, 20, 50]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="count">
			</el-pagination>
		</el-card>



		<!-- 添加用户的对话框 -->
		<el-dialog title="添加角色" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
			<!-- 内容主题区域 -->
			<el-form :model="addForm" :rules="addFromRules" ref="addFormRef" label-width="70px">
				<el-form-item label="角色名称 " prop="Name">
					<el-input v-model="addForm.Name"></el-input>
				</el-form-item>
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="addRole">确定</el-button>
			</span>
		</el-dialog>
		<!-- 修改用户的对话框 -->
		<el-dialog title="修改角色" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
			<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
				<el-form-item label="角色名">
					<el-input v-model="editForm.Name"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="editDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="editRoleInfo">确定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="分配模块" :visible.sync="setRoleDialogVisible" width="50%" @close="setRoleDialogClosed">
			<div>
				<el-tree :data="ModuleList" 
				:props="defaultProps"
				show-checkbox
				ref="tree"
				 node-key="Id"
				default-expand-all
				:default-checked-keys="keys" 
				 @node-click="handleNodeClick"
				@check-change="handleCheckChange"
				></el-tree>
			</div>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="BaoChun">保存</el-button>
			</span>

		</el-dialog>




 </div>
</template>

<script>
  export default {
    data() {
      return {


		  defaultProps: {
					children: 'children',
					label: 'Name'
				},
				// 存放模块列表数据
				ModuleList: [],
				keys:[],
				
				permissionIds:"",
				RoleId:"",



				//获取用户列表的参数对象
				queryInfo: {
					Name:"",
					//当前的页数
				},
				roleslist: [], //table数据
				page: 1,
				//当前每页显示多少条数据
				limit: 10,
				count: 0,
				//控制添加用户对话框的显示与隐藏
				addDialogVisible: false,
				//添加用户的表单数据
				addForm: {
					Name: '',
				},
				//添加表单的验证规则对象
				addFromRules: {
				
				},
				//控制修改用户对话框的显示与隐藏
				editDialogVisible: false,
				//查询到的用户信息对象
				editForm: {},
				// 修改表单的验证规则对象
				editFormRules: {
				},
				//控制分配角色对话框的显示与隐藏
				setRoleDialogVisible: false,
				// 需要被分配角色的用户信息
				userInfo: {},
				//所有角色的数据列表
				roleslist: {},
				//已选中的角色Id值
				selectedRoleId: ''
      }
    },
    created() {
			this.getRoleList()
		},
		methods: {
			async getRoleList() {
				const {
					data: res
				} = await this.$http.get('api/Role/GetRoles?page=' + this.page +
					"&limit=" + this.limit +
					"&Name=" + this.queryInfo.Name )
				console.log(res)
				if (res.code !== 0) {
					return this.$message.error('获取用户列表失败')
				}
				this.roleslist = res.data
				this.count = res.count

			},
			onSubmit(){
				this.getRoleList();
			},
			
			// 监听pagesize改变的事件
			handleSizeChange(newSize) {
				//console.log(newSize)
				this.limit = newSize
				this.getRoleList()
			},
			// 监听页码值改变的事件
			handleCurrentChange(newPage) {
				//console.log(newPage)
				this.page = newPage
				this.getRoleList()
			},
			
			async changeStatus(row) {
				this.switchrow = row;
				const { data: res } = await this.$http.post('api/Role/Updatestatus', this.switchrow);
				console.log(res)
				if (res.code !== 0) {
					this.$message.error(res.msg);
				}
				this.$message.success(res.msg);
				this.getRoleList();
				},
			//监听添加用户对话框关闭事件
			addDialogClosed() {
				this.$refs.addFormRef.resetFields()
			},
			//点击按钮，添加新用户
			addRole() {
				this.$refs.addFormRef.validate(async valid => {
					if (!valid) return
					//可以发起添加用户的网络请求
					const {
						data: res
					} = await this.$http.post('api/Role/AddRoles', this.addForm)
					console.log(res);
					if (res.code !== 0) {
						this.$message.error('添加用户失败!')
					}
					this.$message.success('添加用户成功!')
					//隐藏添加用户的对话框
					this.addDialogVisible = false
					this.getRoleList()
				})
			},
			//展示编辑用户的对话框
			async showEditDialog(Id) {
				console.log(Id)
				const {
					data: res
				} = await this.$http.get('api/Role/GetRolesById?Id=' + Id)
				console.log(res)
				if (res.code !== 0) {
					return this.$message.error('查询用户信息失败!')
				}
				this.editForm = res.data
				this.editDialogVisible = true
			 },
			//监听修改用户对话框的关闭事件
			editDialogClosed() {
				this.$refs.editFormRef.resetFields()
			},
			//修改用户信息并提交
			editRoleInfo() {
				this.$refs.editFormRef.validate(async valid => {
					if (!valid) return
					//发起修改用户信息的数据请求
					const {
						data: res
					} = await this.$http.post('api/Role/GetRolesEdit',this.editForm)
					console.log(res)
					
					if (res.code !== 0) {
						return this.$message.error('更新用户信息失败!')
					}
					//关闭对话框
					this.editDialogVisible = false
					//刷新数据列表
					this.getRoleList()
					//提示修改成功
					this.$message.success('更新用户信息成功!')
				})
			},
			//根据Id删除对应的用户信息
			async removeRoleById(Id) {
				//弹框询问用户是否删除数据
				const confirmResult = await this.$confirm(
					'此操作将永久删除该用户, 是否继续?',
					'提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}
				).catch(err => err)
				//如果用户确认删除，则返回值为字符串 comfirm
				//如果用户取消删除 则返回值为字符串 cancel
				if (confirmResult !== 'confirm') {
					return this.$message.info('已取消删除')
				}
				const {
					data: res
				} = await this.$http.post('api/Role/GetRoleDelById?Id=' + Id)
				console.log(res)
				if (res.code !== 0) {
					return this.$message.error(res.msg)
				}
				this.$message.success('删除用户成功!')
				this.getRoleList()
			},
			//根据Id重置对应的用户信息
			async refreshById(Id) {
				//弹框询问用户是重置
				const confirmResult = await this.$confirm(
					'此操作将重置该用户密码, 默认为123456是否继续?',
					'提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}
				).catch(err => err)
				//如果用户确认删除，则返回值为字符串 comfirm
				//如果用户取消删除 则返回值为字符串 cancel
				if (confirmResult !== 'confirm') {
					return this.$message.info('已取消重置')
				}
				const {
					data: res
				} = await this.$http.get('api/Users/GetUserRefreshById?Id=' + Id)
				console.log(res)
				if (res.code !== 0) {
					return this.$message.error('重置用户失败!')
				}
				this.$message.success('重置用户成功!')
				this.getUserList()
			},
			//展示分配角色的对话框
			async setRole(Id) {
				console.log(Id)
				//this.setRoleDialogVisible = true
				//在展示对话框之前，获取所有角色的列表
				const {
					data: res
				} = await this.$http.post('api/Role/GetMoudulelistById?Id='+Id)
				console.log(res)
				if (res.code !== 0) {
					this.ModuleList=res.data
					this.keys=res.keys
					this.RoleId=res.RoleId
					this.setRoleDialogVisible = true
					return this.$message.success('该用户没有角色 请添加!')		
				}
				this.ModuleList=res.data
				this.keys=res.keys
				this.RoleId=res.RoleId
				this.setRoleDialogVisible = true
			},
			async handleNodeClick(modulesinfo) {
				console.log(modulesinfo);
				//this.editForm = modulesinfo
			},
			
			handleCheckChange () {
				let res = this.$refs.tree.getCheckedNodes()
				let arr = []
				res.forEach((item) => {
					arr.push(item.Id)
				})
				let menuArr = this.unique(arr); // 去除重复的节点
				this.permissionIds = menuArr.join(',')
				console.log(this.permissionIds);
				//console.log(menuArr);
				},


				async BaoChun(){
					const {
					data: res
				} = await this.$http.post('api/Role/GetMouduleRoleAdd?RoleId='+this.RoleId+"&permissionIds="+this.permissionIds)
					console.log(res);
					if (res.code !== 0) {
						return this.$message.success(res.msg)		
					}
					
					this.setRoleDialogVisible = false
					this.getRoleList()
					this.$message.success(res.msg)

				},
				// 数组去重
				unique (arr) {
				let newArr = []
				let len = arr.length
				for (let i = 0; i < len; i++) {
					if (newArr.indexOf(arr[i]) == -1) {
					newArr.push(arr[i])
					}
				}
				return newArr
				},
				



		},
		mounted: function() {
			this.getRoleList()
		},
		computed:function(){
			
		}

  };
</script>


<style Lang="less" scoped>

</style>